<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>元素的获取</title>
</head>
<body>
<div id="div1">div1</div>
<div id="div2">div2</div>
<div class="cls">div3</div>
<div class="cls">div4</div>
<input type="text" name="username"/>
</body>


<script>
    //元素的获取
    //getElementById()  根据id属性值获取元素对象
    let a = document.getElementById("div1");
    alert(a);
    //getElementsByTagName() 根据元素名称获取元素对象,返回数组
    let b = document.getElementsByTagName("div");
    alert(b.length);
    //getElementsByClassName() 根据class属性获取元素对象,返回数组
    let c = document.getElementsByClassName("cls");
    alert(c.length);
    //getElementsByName() 根据name属性获取元素对象,返回数组
    let d = document.getElementsByName("username");
    alert(d.length);
    //子元素对象.parentElement属性  获取当前元素的父元素
    let body = div1.parentElement;
    alert(body);



</script>
</html>